Function.RegisterNamespace("xUnit.js.Extensions.Mocks");

xUnit.js.Extensions.Mocks.Dom=new function(){
    // Public methods
    this.GetDocument=function(document){
        if(!document)document=xUnit.js.Extensions.Stubs.Dom.GetDocument();
        return Mocks.GetMock(Object.Global(),"document",document);
    };

    this.GetDom=function(window){
        if(!window)window=xUnit.js.Extensions.Stubs.Dom.GetWindow();
        function Mockery(during){
            var initialState=collectState();
            var keys=Object.GetKeys(window);
            var mockeries={
                "window":Object.Global()
            };

            for(var i=0;i<keys.length;i++){
                mockeries[keys[i]]=window[keys[i]];
            }
            
            Mocks.GetMocks(Object.Global(),mockeries)(during);
        
            var currentState=collectState();
            for(var i=0;i<currentState.length;i++){
                if(!Array.Contains(initialState,currentState[i])){
                    window[currentState[i]]=Object.Global()[currentState[i]];
                    delete Object.Global()[currentState[i]];
                }
            }
        }
        Mockery.IsMock=true;
        return Mockery;
    };

    this.GetWindow=function(window){
        if(!window)window=xUnit.js.Extensions.Stubs.Dom.GetWindow();
        return Mocks.GetMock(Object.Global(),"window",window);
    };

    this.GetXhr=function(){
        return Mocks.GetMock(Object.Global(),"XMLHttpRequest",xUnit.js.Extensions.Stubs.Dom.GetXhr());
    };

    // Private Methods
    function collectState(){
        var bothersomeMembers=["setTimeout","clearTimeout"];
        return Object.GetKeys(Object.Global()).concat(bothersomeMembers);    
    }
};